﻿/*
 * Author: .net follower (http://dotnetfollower.com)
 * Url: http://bdcrequestlib.codeplex.com/
 * Title: SharePoint Bdc Requests
 * Copyright: � 2012 .net follower 
 * Usage: software is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 */

using System.Data;
using Microsoft.BusinessData.Runtime;
using Microsoft.SharePoint;

namespace SharePointBdcRequests
{
    /// <summary>
    /// Abstracts an interaction with BDC
    /// </summary>
    public abstract class BdcRequestSingle : BdcRequest
    {
        #region fields & properties
        /// <summary>
        /// Gets found data item from external datasource
        /// </summary>
        public virtual IEntityInstance FoundEntityInstance
        {
            get { return EntityInstances.Count > 0 ? EntityInstances[0] : null; }
        }
        public virtual string FoundEntityInstanceEncodedId
        {
            get { return GetEntityInstanceEncodedId(FoundEntityInstance); }
        }
        public virtual object[] FoundEntityInstanceIds
        {
            get { return GetEntityInstanceIds(FoundEntityInstance); }
        }
        #endregion

        public void UpdateBdcColumn(SPListItem spListItem, SPBusinessDataField bdcField)
        {
            // set display value
            string displayValueBdcFieldName = bdcField.GetProperty("BdcField");
            object displayBdcValue          = FoundEntityInstance[displayValueBdcFieldName];
            spListItem[bdcField.Title]      = displayBdcValue;

            // set encoded id
            spListItem[bdcField.RelatedField] = FoundEntityInstanceEncodedId;

            // set secondary fields
            string property = bdcField.GetProperty("SecondaryFieldWssNames");
            string[] secondaryWssFieldNames = SecondaryFieldNamesHelper.Decode(property);

            string secondaryFieldBdcNamesProperty = bdcField.GetProperty("SecondaryFieldBdcNames");
            string[] secondaryFieldBdcNames = SecondaryFieldNamesHelper.Decode(secondaryFieldBdcNamesProperty);

            for (int i = 0; i < secondaryFieldBdcNames.Length; i++)
            {
                object bdcFieldValue = FoundEntityInstance[secondaryFieldBdcNames[i]];
                string secondaryFieldInternalName = secondaryWssFieldNames[i];
                spListItem[secondaryFieldInternalName] = bdcFieldValue;
            }
        }

        #region internal methods
        /// <summary>
        /// Initializes a new instance of the BDCRequest class
        /// </summary>
        /// <remarks>This constructor is called by derived class constructors</remarks>
        protected BdcRequestSingle(BdcRequestParams bdcRequestParams) : base(bdcRequestParams) { }

        protected override DataTable GetDataTable()
        {
            if (FoundEntityInstance == null)
                return new DataTable();
            return FormattedValues ? FoundEntityInstance.EntityAsFormattedDataTable : FoundEntityInstance.EntityAsDataTable;
        }
        #endregion
    }
}
